package com.rchat.pocmini.activity.forgetpwd;

import android.text.TextUtils;
import android.view.View;

import com.aso.common.http.bean.BaseBean;
import com.aso.common.http.delegate.HttpCallbackImpl;
import com.rchat.pocmini.R;
import com.rchat.pocmini.bean.ResetPasswordBean;
import com.rchat.pocmini.bean.VerificationCodeBean;
import com.rchat.pocmini.global.AppGlobal;
import com.rchat.pocmini.global.HttpCodeGlobal;
import com.rchat.pocmini.global.UrlsGlobal;
import com.rchat.pocmini.util.PhoneUtil;

import org.json.JSONException;
import org.json.JSONObject;

import static com.rchat.pocmini.global.HttpCodeGlobal.RES_CODE_431;
import static com.rchat.pocmini.global.HttpCodeGlobal.RES_CODE_439;
import static com.rchat.pocmini.global.HttpCodeGlobal.RES_CODE_441;

/**
 * Created by ljd on 2017/7/28.
 */

public class ForgetPwdPresenterImpl extends ForgetPwdContract.Presenter {

    private boolean isReset = false;

    @Override
    void getVerificationCode(String pwd, String pwdConfirm, String phone) {
        if (!PhoneUtil.isMobileNO(phone)) {
            mView.showShortToast(R.string.register_phone_number_format_error);
            return;
        }
        if (TextUtils.isEmpty(pwd)) {
            mView.showShortToast(R.string.register_password_empty_error);
            return;
        }
        if (pwd.length() < 6) {
            mView.showShortToast(R.string.register_password_less_then_six_error);
            return;
        }
        if (!isSamePassword(pwd, pwdConfirm)) {
            mView.showShortToast(R.string.register_password_enter_twice_no_same);
            return;
        }

        mModel.getVerificationCode(phone, new HttpCallbackImpl<VerificationCodeBean>() {
            @Override
            public void onStart() {
                mView.getVerificationCodeBegin();
            }

            @Override
            public void onSuccess(VerificationCodeBean bean) {
                if (mView == null) {
                    return;
                }
                mView.showShortToast(R.string.register_verification_code_send);
            }

            @Override
            public void onFailureResult(BaseBean bean) {
                if (mView == null) {
                    return;
                }
                mView.showNetError(bean.getCode(), bean.getDesc());
            }

            @Override
            public void onFinish() {

            }
        });
    }


    @Override
    void resetPwd(String verificationCode, String phone, String pwd, String pwdConfirm) {
        if (isReset) {
            return;
        }
        if ("".equals(phone)) {
            mView.showShortToast(R.string.phone_number_empty_error);
            return;
        }

        if ("".equals(verificationCode)) {
            mView.showShortToast(R.string.verification_code_empty_error);
            return;
        }
        if ("".equals(pwd)) {
            mView.showShortToast(R.string.password_empty_error);
            return;
        }
        if (pwd.length() < 6) {
            mView.showShortToast(R.string.register_password_less_then_six_error);
            return;
        }
        if (!isSamePassword(pwd, pwdConfirm)) {
            mView.showShortToast(R.string.register_password_enter_twice_no_same);
            return;
        }

        isReset = true;

        mModel.resetPwd(verificationCode, phone, pwd, new HttpCallbackImpl<ResetPasswordBean>() {
            @Override
            public void onStart() {
                mView.setProcessBarVisibility(View.VISIBLE);
            }

            @Override
            public void onSuccess(ResetPasswordBean bean) {
                if (mView == null) {
                    return;
                }
                isReset = false;
                mView.showShortToast(R.string.password_reset_success);
                mView.finishSelf();
            }

            @Override
            public void onFailureResult(BaseBean bean) {
                if (mView == null) {
                    return;
                }
                isReset = false;
                switch (bean.getCode()) {
                    case RES_CODE_431 + "":
                        mView.showShortToast(R.string.register_verification_code_error);
                        break;
                    case RES_CODE_441 + "":
                        mView.showShortToast(R.string.register_no_get_verification_code_error);
                        break;
                    case RES_CODE_439 + "":
                        mView.showShortToast(R.string.new_password_must_more_than_six);
                        break;
                    default:
                        mView.showNetError(bean.getCode(), bean.getDesc());
                        break;
                }
            }

            @Override
            public void onFinish() {
                if (null == mView) {
                    return;
                }
                mView.setProcessBarVisibility(View.GONE);
            }
        });

    }

    /**
     * 判断两次输入新密码是否相同
     *
     * @param newPassword      新密码
     * @param checkNewPassword 在输一次密码
     * @return 是否相同的布尔值
     */
    private boolean isSamePassword(String newPassword, String checkNewPassword) {
        return newPassword.equals(checkNewPassword);
    }
}
